﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using System.Data.SqlClient;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	public partial class CloseDate: ICloseDate
	{
        public const string SQL_SELECT_YEARMONTH = @"SELECT     CONVERT(varchar(4), Year) + RIGHT('0' + CONVERT(varchar(2), Month), 2) AS Month
                                                        FROM         CloseDate
                                                        ORDER BY Month";
        //Get Year
        [Transaction(TransactionMode.Requires)]
        public DataTable GetCloseDate_Year_DataTable()
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = "SELECT Year FROM CloseDate GROUP BY Year ORDER BY Year ASC";
                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
        }

        public System.DateTime GetTradeMonth_Date(System.DateTime signdue)
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = string.Format("SELECT * FROM CloseDate Where StartDate <= '{0}' AND EndDate >= '{1}'", signdue.ToString("yyyy-MM-dd"),signdue.ToString("yyyy-MM-dd"));
                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                if (dtSearch.Rows.Count == 0)
                    throw new BEGDC.Utility.ExceptionManagement.GDCException("2.0001",string.Format( "最终转正时间:{0}, 没有找到对应的关帐日期, 请联系管理员.",signdue.ToString("yyyy-MM-dd")));

                return Convert.ToDateTime(dtSearch.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Year].ToString() + "-" + dtSearch.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Month].ToString() + "-1");

            }
        }

        /// <summary>
        /// 得到所有的关帐日期, 格式为yyyy-MM
        /// </summary>
        /// <returns></returns>
        public System.Data.DataTable GetCloseDateList()
        {
            using (ISession session = factory.OpenSession())
            {
                string strSql = SQL_SELECT_YEARMONTH;
                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
        }


        //Joan.Lu 2008-09-25
        //Check if close date for current month exists or not
        public bool IsCloseDateSet() 
        {
            using (ISession session = factory.OpenSession())
            {
                string strDate = System.DateTime.Now.ToShortDateString();
                string strSql = "SELECT CloseDateID FROM dbo.CloseDate WHERE '" + strDate  + "' BETWEEN StartDate AND EndDate";
                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);
                if (dtSearch.Rows.Count>0)
                    return true;
                else
                    return false;
            }        
        
        }

        public void SetTradeMonth(int closeDateId)
        {
            using (ISession session = factory.OpenSession())
            {
                ISysRefDate sysRef = (BEGDC.PreContract.BLL.ISysRefDate)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_SysRefDate_Service];

                SqlParameter[] paras = new SqlParameter[1];

                paras[0] = new SqlParameter("@CloseDateID", SqlDbType.Int);
                paras[0].Value = closeDateId;

                BEGDC.Utility.DataAccess.SQLServer.ExecuteDatasetBySp((SqlConnection)session.Connection,
                             "sp_UpdateTradeMonth", paras);
            }
        }

	}
}



